#include "LevelCompleteState.h"

#include "../SGD Wrappers/SGD_String.h"

#include "BitmapFont.h"
#include "MainMenuState.h"
#include "GamePlayState.h"
#include "Game.h"
#include "ProfileManager.h"
#include "LevelManager.h"
#include "LevelMenuState.h"
#include "ProfileManager.h"

LevelCompleteState::LevelCompleteState(void)
{
	cursor = 0;
	LevelCompleteMenuRect.left = Game::GetInstance()->GetScreenWidth() / 2-200;
	LevelCompleteMenuRect.right = Game::GetInstance()->GetScreenWidth() / 2+200;
	LevelCompleteMenuRect.top = 50;
	LevelCompleteMenuRect.bottom = Game::GetInstance()->GetScreenHeight() -100;

	LevelSelectNavID = -1;
	ReplayNavID = -1;
	ContinueNavID = -1;
}


LevelCompleteState::~LevelCompleteState(void)
{
}

void LevelCompleteState::Update(float deltaTime)
{	 
	if(dI->KeyPressed(DIK_UP) || dI->KeyPressed(DIK_W)|| dI->JoystickDPadPressed(DIR_UP) || CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0)
	{
		controllerDelay = 0.25f;
		if(cursor > 0)
			--cursor;
		else
			cursor = 2;
	}
	else if (dI->KeyPressed(DIK_DOWN) || dI->KeyPressed(DIK_S) || dI->JoystickDPadPressed(DIR_DOWN) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.25f;
		if(cursor < 2)
			++cursor;
		else
			cursor = 0;
	}

	if(dI->KeyPressed(DIK_RETURN) || dI->JoystickButtonPressed(0))
	{
		switch(cursor)
		{
		case 0:
			ProfileManager::GetInstance()->saveProfiles();
			if(GamePlayState::GetInstance()->LoadNextLevel())
			{
				GamePlayState::GetInstance()->SetIsOver(false);
				GamePlayState::GetInstance()->GetPlayer()->SetIsOver(false);
				GamePlayState::GetInstance()->GetPlayer()->SetCurTime(0.0f);
			}
			else
			{
				return;
			}
			break;
		case 1:
			ProfileManager::GetInstance()->saveProfiles();
			GamePlayState::GetInstance()->ReplayLevel();
			GamePlayState::GetInstance()->SetIsOver(false);
			GamePlayState::GetInstance()->GetPlayer()->SetIsOver(false);
			GamePlayState::GetInstance()->GetPlayer()->SetCurTime(0.0f);
			break;
		case 2:
			ProfileManager::GetInstance()->saveProfiles();
			Game::GetInstance()->ChangeState(LevelMenuState::GetInstance());
			break;
		}
		return;
	}
}	 
void LevelCompleteState::Render(void) const		
{	
	//D3D->DrawRect(LevelCompleteMenuRect,D3DCOLOR_ARGB(255,150,155,155));
	//BitmapFont::GetInstance()->Print("LEVEL COMPLETE!", LevelCompleteMenuRect.left + (LevelCompleteMenuRect.right-LevelCompleteMenuRect.left)/2 -145.0f, (float)LevelCompleteMenuRect.top ,0, D3DCOLOR_ARGB(255,30,40,50));

	//Draw the selection
	switch (cursor)
	{
	case 0:
		{
			if( this->ContinueNavID != -1 )
				tM->Draw(ContinueNavID, LevelCompleteMenuRect.left, LevelCompleteMenuRect.top, 0.5f, 0.5f );
			break;
		}
	case 1:
		{
			if( this->ReplayNavID != -1 )
				tM->Draw(ReplayNavID, LevelCompleteMenuRect.left, LevelCompleteMenuRect.top, 0.5f, 0.5f );
			break;
		}
	case 2:
		{
			if( this->LevelSelectNavID != -1 )
				tM->Draw(LevelSelectNavID, LevelCompleteMenuRect.left, LevelCompleteMenuRect.top, 0.5f, 0.5f );
			break;
		}
	default:
		break;
	}

	ostringstream oss;

	oss << "Your Time : " << GamePlayState::GetInstance()->GetPlayer()->GetCurTime();
	BitmapFont::GetInstance()->Print(oss.str(), (float)(LevelCompleteMenuRect.left + (LevelCompleteMenuRect.right-LevelCompleteMenuRect.left)/2 -145.0f), (float)LevelCompleteMenuRect.top + 100 , 0, D3DCOLOR_ARGB(255,30,40,50));
	oss.str("");

	oss << "Best Time : " << ProfileManager::GetInstance()->getCurProfile().getBestTime(GamePlayState::GetInstance()->GetlevelManager().GetCurLevel());
	BitmapFont::GetInstance()->Print(oss.str(), (float)(LevelCompleteMenuRect.left + (LevelCompleteMenuRect.right-LevelCompleteMenuRect.left)/2 -145.0f), (float)LevelCompleteMenuRect.top + 145 , 0, D3DCOLOR_ARGB(255,30,40,50));
	oss.str("");

	//RECT renderRect;
	//for(int curMenuItem = 0; curMenuItem < 3; ++curMenuItem)
	//{
	//	renderRect.left = LevelCompleteMenuRect.left + 50;
	//	renderRect.right = LevelCompleteMenuRect.right - 50;
	//	renderRect.top  = (LevelCompleteMenuRect.top + 150) + 60*curMenuItem +30*curMenuItem;
	//	renderRect.bottom= (LevelCompleteMenuRect.top + 225) + 60*curMenuItem + 30*curMenuItem;
	//	D3D->DrawRect(renderRect, D3DCOLOR_ARGB(255,170,0,0));

	//	switch(curMenuItem)
	//	{
	//	case 0:
	//		BitmapFont::GetInstance()->Print("Continue", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,240,200,0));
	//		break;																		  	
	//	case 1:																			  	
	//		BitmapFont::GetInstance()->Print("Replay", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,240,200,0));
	//		break;																		  	 
	//	case 2:																			  	 
	//		BitmapFont::GetInstance()->Print("Level Select", (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,240,200,0));
	//		break;																		   	  
	//	}
	//}

	//RECT selectedRect;
	//selectedRect.left = LevelCompleteMenuRect.left + 50;
	//selectedRect.right = LevelCompleteMenuRect.right - 50;
	//selectedRect.top  = (LevelCompleteMenuRect.top + 150) + 60*cursor +30*cursor;
	//selectedRect.bottom= (LevelCompleteMenuRect.top + 225) + 60*cursor + 30*cursor;	
	//D3D->DrawHollowRect(selectedRect,D3DCOLOR_ARGB(255,255,255,0),3);
}	

void LevelCompleteState::Enter(void)			
{	
	GameState::Enter();

	// unlock the next level
	if(GamePlayState::GetInstance()->GetlevelManager().GetCurLevel() == ProfileManager::GetInstance()->getCurProfile().getLevelsUnlocked())
		ProfileManager::GetInstance()->getCurProfile().setLevelsUnlocked(ProfileManager::GetInstance()->getCurProfile().getLevelsUnlocked() + 1);

	LevelSelectNavID = CSGD_TextureManager::GetInstance()->LoadTexture((_T("Textures\\Menu\\LevelComLevelSel.png")));
	ReplayNavID = CSGD_TextureManager::GetInstance()->LoadTexture((_T("Textures\\Menu\\LevelComReplay.png")));
	ContinueNavID = CSGD_TextureManager::GetInstance()->LoadTexture((_T("Textures\\Menu\\LevelComContinue.png")));

}	
void LevelCompleteState::Exit(void)				
{
	GameState::Exit();
}